package com.vivo.tasks.vivofx;

import com.vivo.common.DatabaseContextHolder;
import com.vivo.fx.material.entity.FxMaterialinfo;
import com.vivo.fx.material.entity.FxMaterialkind;
import com.vivo.fx.material.service.FxMaterialinfoService;
import com.vivo.fx.material.service.FxMaterialkindService;
import com.vivo.material.entity.ViMaterialinfo;
import com.vivo.material.entity.ViMaterialkind;
import com.vivo.material.service.ViMaterialinfoService;
import com.vivo.material.service.ViMaterialkindService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class TaskMaterial {

    @Autowired
    private FxMaterialkindService fxMaterialkindService;

    @Autowired
    private FxMaterialinfoService fxMaterialinfoService;

    @Autowired
    private ViMaterialkindService viMaterialkindService;

    @Autowired
    private ViMaterialinfoService viMaterialinfoService;

    public void SyncData() {

        GetMaterialKind("ah");
        GetMaterialKind("js");


    }


    private void GetMaterialKind(String area) {
        System.out.println("get data from " + area + " db start...");

        List<FxMaterialkind> fxMaterialkindList = null;

        try {
            // 切换数据库
            Helper.changeDB(area);

            fxMaterialkindList = fxMaterialkindService.findList(new FxMaterialkind());

        } finally {
            DatabaseContextHolder.setMainDb();
        }

        for (FxMaterialkind mk: fxMaterialkindList) {
            ViMaterialkind vk = new ViMaterialkind();
            vk.setId(mk.getMaterialkindid());
            vk.setIsTaskInsert(true);

            vk.setMaterialkindattr(mk.getMaterialkindattr());
            vk.setMaterialkindcode(mk.getMaterialkindcode());
            vk.setMaterialkindname(mk.getMaterialkindname());
            vk.setMaterialkinddescription(mk.getMaterialkinddescription());

            viMaterialkindService.save(vk);
        }

        System.out.println("get data from " + area + " db end.");
    }


    private void GetMaterialInfo(String area) {
        System.out.println("get data from " + area + " db start...");

        List<FxMaterialinfo> fxMaterialinfoList = null;

        try {
            // 切换数据库
            Helper.changeDB(area);

            fxMaterialinfoList = fxMaterialinfoService.findList(new FxMaterialinfo());

        } finally {
            DatabaseContextHolder.setMainDb();
        }

        for(FxMaterialinfo fi: fxMaterialinfoList) {
            ViMaterialinfo vi = new ViMaterialinfo();
            vi.setId(fi.getMaterialinfoid());
            vi.setIsTaskInsert(true);

            vi.setMaterialkindid(fi.getMaterialkindid());
            vi.setLevelcode(fi.getLevelcode());
            vi.setMaterialcode(fi.getMaterialcode());
            vi.setMaterialname(fi.getMaterialname());

            vi.setMaerialunit(fi.getMaerialunit());
            vi.setMaterialstandard(fi.getMaterialstandard());
            vi.setPurchaseprice(fi.getPurchaseprice());
            vi.setDispatchprice(fi.getDispatchprice());
            vi.setBalanceprice(fi.getBalanceprice());

            vi.setChargeoffpercent(fi.getChargeoffpercent());
            vi.setUseperiod(fi.getUseperiod());
            vi.setIssuestartdate(fi.getIssuestartdate());
            vi.setIssueenddate(fi.getIssueenddate());
            vi.setMaterialdescription(fi.getMaterialdescription());

            vi.setMaterialproperty(fi.getMaterialproperty());
            vi.setMaterialnature(fi.getMaterialnature());
            vi.setMaterialquality(fi.getMaterialquality());
            vi.setMaterialtype(fi.getMaterialtype());
            vi.setMaterialcontent(fi.getMaterialcontent());

            vi.setEnablechange(fi.getEnablechange());
            vi.setIstakestock(fi.getIstakestock());
            vi.setEnableretrieve(fi.getEnableretrieve());
            vi.setIscheckup(fi.getIscheckup());
            vi.setIsacceptance(fi.getIsacceptance());

            vi.setIsphotoacceptance(fi.getIsphotoacceptance());
            vi.setIstraceback(fi.getIstraceback());
            vi.setIsNewRecord(fi.getIsNewRecord());
            vi.setIstotalstock(fi.getIstotalstock());
            vi.setIsneedcode(fi.getIsneedcode());

            vi.setPicturesize(fi.getPicturesize());
            vi.setRemark(fi.getRemark());
            vi.setMaterialstate(fi.getMaterialstate());
            vi.setMindispatchcount(fi.getMindispatchcount());
            vi.setMaterialphoto(fi.getMaterialphoto());

            vi.setIsoffsetadvertising(fi.getIsoffsetadvertising());

            viMaterialinfoService.save(vi);
        }

        System.out.println("get data from " + area + " db end.");
    }
}
